package paas.storage.serviceTest;

import org.mortbay.util.ajax.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import paas.storage.distributedColumnDatabase.IConnection;
import paas.storage.distributedColumnDatabase.ITable;
import paas.storage.result.*;
import paas.storage.util.JSONHelper;

import java.util.Map;

/**
 * 测试表的操作
 *
 * @author xufeng
 * @email 525207937@qq.com
 * @date 2021/1/26 14:33
 */
@Service
public class ITableTest {
    private static Logger log = LoggerFactory.getLogger(ITableTest.class);

    @Autowired
    private ITable iTable;
    @Autowired
    private IConnection iConnection;

    /**
     * 操作表的接口汇总测试
     * 1.创建连接
     * 2.创建之前先删除之前的表方便演示
     * 3.创建表
     * 4.删除刚刚创建的表
     * 5.重建刚刚那张表并修改表数据
     * 6.获取数据库下的表列表, 根据正则表达式获取表列表
     * 7.查询表信息
     * 8.判断表是否存在表参数
     */
   public void all() {
        Map map = (Map) JSONHelper.ResolveJsonFileToObject("test.json");
        //1.创建连接
        CreateResponse createResponse = iConnection.create("", map.get("token").toString(), map.get("config").toString());
        //618532e7-e1f3-4ffd-83d0-3a23c56318bb
        String connectionId = createResponse.getConnectionId();
        Map<String, Object> tableCreate = (Map<String, Object>) map.get("tableCreate");
        log.info("创建表开始");
        String database = tableCreate.get("database").toString();

        String table = tableCreate.get("table").toString();
        //2 创建之前先删除之前的表方便演示
        iTable.delete(connectionId, database, "test[0-9]*");
        //列簇信息
        String columnFamilyList = tableCreate.get("columnFamilyList").toString();
        //分区数
        int numRegions = (int) tableCreate.get("numRegions");
        String splitMethod = tableCreate.get("splitMethod").toString();
        int regionReplicatio = (int) tableCreate.get("regionReplicatio");

        log.info("5.2.2.1创建表参数,database={},table={},columnFamilyList={},numRegions{},splitMethod{},regionReplicatio{}", database, table, columnFamilyList, numRegions, splitMethod, regionReplicatio);
        //3.创建表
        TableCreateResponse tableCreateResponse = iTable.create(connectionId, database, table, columnFamilyList, numRegions, splitMethod, regionReplicatio, "");
        log.info("5.2.2.1创建表返回结果,{}", JSON.toString(tableCreateResponse));

        //4.删除刚刚创建的表
        log.info("5.2.2.2删除创建的表参数,参数database={},table={}", database, table);
        TableDeleteResponse tableDeleteResponse = iTable.delete(connectionId, database, table);
        log.info("5.2.2.2删除表结果,{}" + tableDeleteResponse.toString());
        //4.正则表达式删除表
        /*table = "test[0-9]*";
        TableDeleteResponse tableDeleteResponse1 = iTable.delete(connectionId, database, table);
        log.info("删除表结果,{}" + tableDeleteResponse1.toString());*/
        //
        log.info("5.2.2.3新建一张表,{}" + tableDeleteResponse.toString());
        tableCreateResponse = iTable.create(connectionId, database, table, columnFamilyList, numRegions, splitMethod, regionReplicatio, "");
        log.info("5.2.2.1创建表返回结果,{}", JSON.toString(tableCreateResponse));

        Map<String, Object> updateCreate = (Map<String, Object>) map.get("updateCreate");

        columnFamilyList = updateCreate.get("columnFamilyList").toString();
        log.info("5.2.2.3修改表参数,database={},table={},columnFamilyList={},numRegions{},splitMethod{},regionReplicatio{}", database, table, columnFamilyList, numRegions, splitMethod, regionReplicatio);

        numRegions = (int) updateCreate.get("numRegions");
        splitMethod = updateCreate.get("splitMethod").toString();
        regionReplicatio = (int) updateCreate.get("regionReplicatio");
        //5修改表数据
        TableUpdateResponse tableUpdateResponse = iTable.update(connectionId, database, table, columnFamilyList, 1, numRegions, splitMethod, regionReplicatio, "");
        log.info("5.2.2.3更新表结果,{}", tableUpdateResponse.toString());

        //6.测试获取数据库下的表列表, 测试根据正则表达式获取表列表
        table = "";
        log.info("5.2.2.4获取数据库下所有表列表参数,database = {},table={}", database, table);
        TableListResponse tableListResponse = iTable.list(connectionId, database, table);
        log.info("5.2.2.4返回数据库下所有表列表,{}", tableListResponse.toString());
        //根据正则查询表
        table = "test[0-9]*";
        log.info("5.2.2.4根据表名正则获取匹配表参数,database = {},table={}", database, table);
        TableListResponse tableListResponse1 = iTable.list(connectionId, database, table);
        log.info("5.2.2.4根据表名正则获取所有表列表,{}", tableListResponse1.toString());


        //7.查询表信息
        table = map.get("findTableInfo").toString();
        log.info("5.2.2.5获取表信息参数,database = {},table={}", database, table);
        TableQueryResponse tableQueryResponse = iTable.query(connectionId, database, table);
        log.info("5.2.2.4获取表信息返回结果,{}", tableQueryResponse.toString());

        //8.判断是否存在
        table = map.get("tableExists").toString();
        log.info("判断表是否存在参数,database = {},table={}", database, table);
        TableExistResponse tableExistResponse = iTable.exist(connectionId, database, table);
        log.info("判断表是否存在,{}", tableExistResponse.toString());
    }

}
